package util;
//20190210423赵扬

public class Pagination {
    //每页显示的元组数
    private int pageSize = 5;
    //页数
    private int pageNo = 1;
    //总元组数
    private int totalNum = 1;

    /**
     * 为findAll方法构建 LIMIT子句，以便分页查询
     * @param totalNum 总元组数
     * @return
     */
    public String toLimitClause(int totalNum){
        //设置总元组数
        this.totalNum = totalNum;
        //设置偏移量
        int offset = (this.pageNo-1)*this.pageSize;
        //返回构建完成LIMIT子句
        return " LIMIT " + offset + " , " + this.pageSize;
    }

    /**
     * 只有含参构造器，防止JSON默认调用无参构造器
     * @param pageSize 每页显示的元组数
     * @param pageNo 页数
     */
    public Pagination(int pageSize,int pageNo){
        //防止错误赋值
        if (pageSize>0){
            this.pageSize = pageSize;
        }else {
            this.pageSize = 5;
        }
        if (pageNo>0){
            this.pageNo = pageNo;
        }else {
            this.pageNo = 1;
        }
    }
    public int getPageSize(){
        return this.pageSize;
    }

    public int getPageNo(){
        return this.pageNo;
    }
    public int getTotalNum(){
        return this.totalNum;
    }
}
